<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\HouseBroker;
use App\Models\BrokerCompany;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class HouseBrokerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HouseBroker(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('broker_name');
            $grid->column('broker_image','头像')->image();
            $grid->column('broker_phone');
            $grid->column('new_house');
            $grid->column('two_house');
            $grid->column('rental_housing');
            $grid->column('sales_volume','成交量');
            $grid->column('company')->display(function ($grid){
                $company = BrokerCompany::where('id',$grid)->first()->toArray();
                return $company['company_name'];
            });
            $grid->column('state')->display(function ($grid){
                if ($grid==1){
                    return "在职";
                }else{
                    return "离职";
                }
            })->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HouseBroker(), function (Show $show) {
            $show->field('id');
            $show->field('broker_name');
            $show->field('broker_phone');
            $show->field('broker_image','头像')->image();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HouseBroker(), function (Form $form) {
            $form->display('id');
            $form->text('broker_name');
            $form->text('broker_phone');
            $form->image('broker_image','头像')->disk('qiniu');
            $form->multipleSelectTable('company')->from(\App\Admin\Actions\Post\BrokerCompany::make())->saving(function ($e){
                $e=implode(',',$e);
                return $e;
            });;
            $form->display('created_at');
            $form->display('updated_at');
//            $form->saving(function (Form $form){
//               if ($form->isEditing()){
//                   $form->deleteInput('');
//               }
//               if ($form->isCreating()){
//
//               }
//            });
//            $form->saved(function (Form $form){
//
//            });
        });
    }
}
